<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<!--- custom tag for rendering form elements --->

<cfif NOT thisTag.hasEndTag>
	<cfthrow type="mod.formutils.field.NoEndTag" message="FormUtils.field custom tag was called without an end tag." detail="You must use the formutils.field custom tag with an end tag. we're XHTML compliant :)" />
</cfif>
<cfsetting enablecfoutputonly="yes">

<cfparam name="attributes.field" default="" />
<cfparam name="attributes.id" default="" />
<cfparam name="attributes.name" default="#attributes.id#" />
<cfparam name="attributes.value" default="" />
<cfparam name="attributes.title" default="" />
<cfparam name="attributes.style" default="" />
<cfparam name="attributes.class" default="" />
<cfparam name="attributes.onclick" default="" />
<cfparam name="attributes.onchange" default="" />
<cfparam name="attributes.size" default="" />
<cfparam name="attributes.showOptions" default="label,field,container" />

<cfset showContainer = yesNoFormat(listFindNoCase(attributes.showOptions, 'container')) />
<cfset showLabel = yesNoFormat(listFindNoCase(attributes.showOptions, 'label')) />
<cfset showField = yesNoFormat(listFindNoCase(attributes.showOptions, 'field')) />
<cfset outStr = "" />

<cfif NOT isObject(attributes.field)>
	<cfthrow type="mod.formutils.field.NoFieldBean" message="FormUtils.Field custom tag was called without Field." detail="You must supply the a field bean to field.cfm." />
</cfif>

<cfif thisTag.executionMode EQ 'end'>
	<cfset thisField = attributes.field />
    <cfif NOT len(trim(thisField.getFieldName()))>
    	<cfthrow type="mod.formutils.field.FieldNotFound" message="FormUtils.Field custom tag couldn't find field #attributes.fieldId#." detail="You must supply a valid primary key of a field from the database." />
    </cfif>
    
	<cfif showContainer>
        <cfset outStr &= '<div class="ctrlHolder">' />
    </cfif>
	<cfif showLabel>
		<cfset outStr &= '<label for="' & thisField.getFieldName() & '" class="' & thisField.getlabelClass() & '">' & thisField.getLabelText() & '</label>' />
    </cfif>
    
    <cfset optStr = "" />
    
    <cfif len(thisField.getFieldClass())>
    	<cfset attributes.class = listAppend(attributes.class, thisField.getFieldClass, " ") />
    </cfif>
    
    <cfif len(attributes.class)>
    	<cfset optStr &= ' class="' & attributes.class & '"' />
    </cfif>
    
    <cfif isValid('integer', thisField.getDataMaxLength()) AND listFindNoCase('text,hidden,textarea', thisField.getFieldType())>
    	<cfset optStr &= ' maxlength="' & thisField.getDataMaxLength() & '"' />
    </cfif>
    
    <cfif len(trim(attributes.title))>
    	<cfset optStr &= ' title="' & replaceNoCase(trim(attributes.title), '"', '&quot;', 'all') & '"' />
    </cfif>
    
    <cfif len(trim(attributes.style))>
    	<cfset optStr &= ' style="' & trim(attributes.style) & '"' />
    </cfif>
    
    <cfif len(trim(attributes.onclick))>
    	<cfset optStr &= ' onclick="' & trim(attributes.onclick) & '"' />
    </cfif>
	
	<cfif len(trim(attributes.onchange))>
    	<cfset optStr &= ' onchange="' & trim(attributes.onchange) & '"' />
    </cfif>
    
    <cfif len(trim(attributes.size))>
    	<cfset optStr &= ' size="' & trim(attributes.size) & '"' />
    </cfif>
    
    
    <cfif showField>
    <cfswitch expression="#thisField.getFieldType()#">
    	<cfcase value="text">
            <cfset outStr &= '<input type="text" id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '" value="' & form["#thisField.getFieldName()#"] & '"' & optStr & '/>' />
        </cfcase>
        
        <cfcase value="hidden">
            <cfset outStr &= '<input type="hidden" id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '" value="' & form["#thisField.getFieldName()#"] & '"' & optStr & ' />' />
        </cfcase>
        
        <cfcase value="select">
			<cfset outStr &= '<select id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '"' & optStr & '>' />
            <cfset outStr &= '<option value=""></option>' />
            <cfset theseOptions = thisField.getOptions() />
            <cfloop from="1" to="#arrayLen(theseOptions)#" index="iii">
                <cfset outStr &= '<option value="' & theseOptions[iii].getOptionValue() & '"' />
                <cfif form["#thisField.getFieldName()#"] EQ theseOptions[iii].getOptionValue()>
                    <cfset outStr &= ' selected' />
                </cfif>
                <cfset outStr &= '>' />
                <cfif len(trim(theseOptions[iii].getOptionLabel()))>
                    <cfset outStr &= theseOptions[iii].getOptionLabel() />
                <cfelse>
                    <cfset outStr &= theseOptions[iii].getOptionValue() />
                </cfif>
            </cfloop>
            <cfif len(trim(thisField.getContentProvider()))>
            	<cftry>
                	<cfset outStr &= evaluate(replace(thisField.getContentProvider(), '()', '("'&form["#thisField.getFieldName()#"]&'")')) />
                    <cfcatch type="any"><cfdump var="#cfcatch#"/><cfabort></cfcatch>
                </cftry>
            </cfif>
            <cfset outStr &= '</select>' />
        </cfcase>
        
        <cfcase value="radio,radio other">
			<cfset outStr &= '<div' & iif(len(thisField.getFieldClass()), de(' class="#thisField.getFieldClass()#"'), de('')) & '>' />
            <cfset theseOptions = thisField.getOptions() />
            <cfloop from="1" to="#arrayLen(theseOptions)#" index="iii">
                <cfset outStr &= '<input type="radio" id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '" value="' & theseOptions[iii].getOptionValue() & '"' & optStr />
                 <cfif form["#thisField.getFieldName()#"] EQ theseOptions[iii].getOptionValue()>
                    <cfset outStr &= ' checked="checked"' />
                </cfif>
                <cfset outStr &= ' /><span class="optLabel"> ' />
                <cfif len(trim(theseOptions[iii].getOptionLabel()))>
                    <cfset outStr &= theseOptions[iii].getOptionLabel() />
                <cfelse>
                    <cfset outStr &= theseOptions[iii].getOptionValue() />
                </cfif>
                <cfset outStr &= '</span>' />
            </cfloop>
            <cfif thisField.getFieldType EQ 'radio other'>
                <cfset outStr &= '<input type="radio" id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '" value="0" class="' & optStr & '"' />
                 <cfif form["#thisField.getFieldName()#"] EQ '0'>
                    <cfset outStr &= ' checked="checked"' />
                </cfif>
                <cfset outStr &= ' /><span class="optLabel"> Other</span>' />
                <cfset outStr &= '<input type="text" id="' & thisField.getFieldName() & '_otherVal" name="' & thisField.getFieldName() & '_otherVal" value="' & form["#thisField.getFieldName()#_otherVal"] & '" class="otherText" />' />
            </cfif>
            <cfset outStr &= '</div>' />
        </cfcase>
        
        <cfcase value="checkbox,checkbox other">
			<cfset outStr &= '<div' & iif(len(thisField.getFieldClass()), de(' class="#thisField.getFieldClass()#"'), de('')) & '>' />
            <cfset theseOptions = thisField.getOptions() />
            <cfloop from="1" to="#arrayLen(theseOptions)#" index="iii">
                <cfset outStr &= '<input type="checkbox" id="' & thisField.getFieldName() & '_#iii#" name="' & thisField.getFieldName() & '_#iii#" value="' & theseOptions[iii].getOptionValue() & '"' & optStr />
                 <cfif form["#thisField.getFieldName()#_#iii#"] EQ theseOptions[iii].getOptionValue()>
                    <cfset outStr &= ' checked="checked"' />
                </cfif>
                <cfset outStr &= ' /><span class="optLabel"> ' />
                <cfif len(trim(theseOptions[iii].getOptionLabel()))>
                    <cfset outStr &= theseOptions[iii].getOptionLabel() />
                <cfelse>
                    <cfset outStr &= theseOptions[iii].getOptionValue() />
                </cfif>
                <cfset outStr &= '</span>' />
            </cfloop>
            <cfif thisField.getFieldType EQ 'checkbox other'>
                <cfset outStr &= '<input type="checkbox" id="' & thisField.getFieldName() & '_other" name="' & thisField.getFieldName() & '_other" value="0" class="' & thisField.getFieldClass() & '"' />
                 <cfif form["#thisField.getFieldName()#_other"] EQ '0'>
                    <cfset outStr &= ' checked="checked"' />
                </cfif>
                <cfset outStr &= ' /><span class="optLabel"> Other</span>' />
                <cfset outStr &= '<input type="text" id="' & thisField.getFieldName() & '_otherVal" name="' & thisField.getFieldName() & '_otherVal" value="' & form["#thisField.getFieldName()#_otherVal"] & '" class="otherText" />' />
            </cfif>
            <cfset outStr &= '</div>' />
        </cfcase>
        
        <cfcase value="text area">
            <cfset outStr &= '<textarea id="' & thisField.getFieldName() & '" name="' & thisField.getFieldName() & '" class="' & optStr & '">' & form["#thisField.getFieldName()#"] & '</textarea>' />
        </cfcase>
        
        <cfcase value="likert">
			<cfset outStr &= '<table class="likertTbl"><tr><th>&nbsp;</th>' />
            <cfset theseOptions = thisField.getOptions() />
            <cfloop from="1" to="#arrayLen(theseOptions)#" index="iii">
                <cfset outStr &= '<th>' & iif(len(trim(theseOptions[iii].getOptionLabel())), de(theseOptions[iii].getOptionLabel()), de(theseOptions[iii].getOptionValue())) & '</th>' />
            </cfloop>
            <cfset outStr &= '</tr>' />
            <cfset theseQuestions = thisField.getQuestions() />
            <cfloop from="1" to="#arrayLen(theseQuestions)#" index="jjj">
                <cfset outStr &= '<tr><td class="likertQuestion">' & theseQuestions[jjj].getQuestionText() & '</td>' />
                <cfloop from="1" to="#arrayLen(theseOptions)#" index="iii">
                    <cfset outStr &= '<td align="center">' />
                    <cfset outStr &= '<input type="radio" id="' & thisField.getFieldName() & '_#jjj#" name="' & thisField.getFieldName() & '_#jjj#" value="' & theseOptions[iii].getOptionValue() & '" class="' & optStr & '"' />
                     <cfif form["#thisField.getFieldName()#_#jjj#"] EQ theseOptions[iii].getOptionValue()>
                        <cfset outStr &= ' checked="checked"' />
                    </cfif>
                    <cfset outStr &= ' />' />
                    <cfset outStr &= '</td>' />
                </cfloop>
                <cfset outStr &= '</tr>' />
            </cfloop>
            <cfset outStr &= '</table>' />
        </cfcase>
        
        <cfdefaultcase>
            <cfset outStr &= thisField.getLabelText() />
        </cfdefaultcase>
    </cfswitch>
    </cfif>
	<cfif showContainer>
        <cfset outStr &= '</div>' />
    </cfif>
	<cfoutput>#outStr#</cfoutput>
</cfif>
<cfsetting enablecfoutputonly="no">
